'use strict';

import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
  indexAction(){
    //auto render template file index_index.html
    return this.display();
  }
  //列表
  async listAction(){
  	try{
  		let page = this.get('page') || 0;
  		let pageSize = this.get('pageSize') || 10;
  		let goodsList = await this.model('home').findByPage('goods_repertory',{}, page, pageSize);
  		return this.success(goodsList);
  	}catch(e){
  		return this.fail(500);
  	}
  }
  // 新增
  async addAction(){
  	try{
  		let newData=this.post();
  		if (!think.isEmpty(newData.id)) {
  			return this.fail(4);
  		}
  		let id = await this.model('home').addOne('goods_repertory', newData);
  		return this.success({id});
  	}catch(e){
  		return this.fail(500);
  	}
  }
  // 删除
  async delAction(){
  	try{
  		let arr = this.post('delarr[]')
		await this.model('home').deleteRecord('goods_repertory', {
			id: ["IN", arr]
		});
  		return this.success();
  	}catch(e){
  		return this.fail(500);
  	}
  }
  // 修改
  async updateAction(){
  	try{
  		let d = this.post();
  		if (think.isEmpty(d.id)) {
  			return this.fail(4);
  		}
		await this.model('home').updateOne('goods_repertory', d.id, d);
  		return this.success();
  	}catch(e){
  		return this.fail(500);
  	}
  }
}